package com.fsi.client;

import java.util.Date;

import com.fsi.client.util.MyDate;
import com.fsi.client.util.MyList;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.TextBox;

public class NewCustomerGUI extends ServiceGUI {
	private final Label title = new Label("New Customer Page");
	private Grid formGrid;
	private final TextBox emailAddressEd;
	private final TextBox userNameEd;
	private final TextBox passwordEd;
	private final MyDate birthdateEd;
	private final MyList genderEd;
	private final MyList countryEd;
	private final TextBox postCodeEd;
	private final Button saveBtn;

	private String loginEmail;

	public void setLoginEmail(String loginEmail) {
		this.loginEmail = loginEmail;
	}

	public NewCustomerGUI() {
		userNameEd = new TextBox();
		passwordEd = new TextBox();
		emailAddressEd = new TextBox();
		birthdateEd = new MyDate();
		genderEd = new MyList(Customer.GENDERS);
		countryEd = new MyList(Customer.COUNTRIES);
		postCodeEd = new TextBox();

		saveBtn = new Button("Save Changes");

		buildForm();
		this.add(title);
		this.add(formGrid);
		this.add(saveBtn);

		wireEvents();
	}

	private void wireEvents() {
		saveBtn.addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {
				addCustomer();
			}
		});
	}

	private void buildForm() {
		formGrid = new Grid(7, 2);

		formGrid.setWidget(0, 0, new Label("User Name"));
		formGrid.setWidget(0, 1, userNameEd);

		formGrid.setWidget(1, 0, new Label("Password"));
		formGrid.setWidget(1, 1, passwordEd);

		formGrid.setWidget(2, 0, new Label("Email Address"));
		formGrid.setWidget(2, 1, emailAddressEd);

		formGrid.setWidget(3, 0, new Label("Birthdate"));
		formGrid.setWidget(3, 1, birthdateEd);

		formGrid.setWidget(4, 0, new Label("Gender"));
		formGrid.setWidget(4, 1, genderEd);

		formGrid.setWidget(5, 0, new Label("Country"));
		formGrid.setWidget(5, 1, countryEd);

		formGrid.setWidget(6, 0, new Label("Postcode"));
		formGrid.setWidget(6, 1, postCodeEd);
	}

	private void addCustomer() {
		String tempName = userNameEd.getText().trim();
		String tempPassword = passwordEd.getText().trim();
		String tempEmail = emailAddressEd.getText().trim();
		int tempGender = genderEd.getSelectedIndex();
		int tempCountry = countryEd.getSelectedIndex();
		String tempPostCode = postCodeEd.getText().trim();
		Date tempBirthdate = birthdateEd.getDate();

		saveBtn.setEnabled(false);
		service.addCustomer(this, new Customer(loginEmail, tempName,
				tempPassword, tempEmail, tempGender, tempCountry, tempPostCode,
				tempBirthdate));
	}

	void setEmail(String email) {
		emailAddressEd.setText(email);
	}

	// ****************************
	// ************ service methods
	// ****************************
	@Override
	public void service_addFailed(String errorMsg) {
		gui.setStatus(errorMsg);
		saveBtn.setEnabled(true);
	}

	@Override
	public void service_addSucceeded(Customer customer) {
		saveBtn.setEnabled(true);
		gui.setStatus("Customer saved");
		gui.showEditCustomer(true);
	}

}
